// pages/makeAppointment/index.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    campus: [], // 校区
    NEXT: '下一步',
    infoArray: [], // 课程
    campusId: '', // 校区Id;
    campusName: '', // 校区名称
    projectArray: [], // 课程数组
    projectName: [] // 课程名称
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCampus()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  // 获取节点
  selectnode(idx, left) {
    wx
      .createSelectorQuery()
      .selectAll('.scroll-view-item')
      .fields({
          dataset: true,
          size: true,
          scrollOffset: true,
          properties: ['scrollX', 'scrollY'],
          computedStyle: ['margin', 'backgroundColor']
        },
        res => {
          this.swiper(res[idx].width, left)
        }
      )
      .exec()
  },
  //  初始化scroll 获取index和距离scroll的左边距
  initScroll(e) {
    const left = e.target.offsetLeft
    const idx = e.currentTarget.dataset.index
    const id = this.data.campus[idx].id
    const campus = this.changeColor(idx)
    this.setData({
      campus
    })

    this.selectnode(idx, left)
    this.getCurricula(id)
  },
  // 改变颜色
  changeColor(idx) {
    idx = parseInt(idx)
    const campus = this.data.campus
    const result = []
    campus.map((item, index) => {
      item.swiperCurrent = index === idx
      result.push(item)
    })
    return result
  },
  // 计算滚动距离
  swiper(width, left) {
    // 获取屏幕宽度
    const pageX = wx.getSystemInfoSync().windowWidth
    const scrollLeft = left - pageX / 2 + width / 2
    this.setData({
      scrollLeft
    })
  },
  // 下一步
  handleNext(e) {
    this._initProjectArray()
    this._initCampusInfo()
    const campus_id = this.data.campusId
    const campus_name = this.data.campusName
    const curricula_id = this.data.projectArray
    const project_name = this.data.projectName
    app.util.formid(e.detail.detail.formId)
    wx.navigateTo({
      url: `/pages/makeAppointment/completeInfo/index?campus_id=${campus_id}&campus_name=${campus_name}&curricula_id=${JSON.stringify(curricula_id)}&project_name=${JSON.stringify(project_name)}`
    })
  },
  // 改变课程选项
  handChangeSelect(e) {
    const list = this.data.infoArray
    const name = e.detail.type_name
    const result = []
    list.map(item => {
      item = item.type_name === name ? e.detail : item
      result.push(item)
    })
    this.setData({
      infoArray: result
    })
  },

  // 初始化被选课程Id
  _initProjectArray() {
    const list = this.data.infoArray
    const result = []
    const names = []
    list.map(item => {
      item.list.map(item => {
        if (item.checked) {
          result.push(item.id)
          names.push(item.cuur_name)
        }
      })
    })
    this.setData({
      projectArray: result,
      projectName: names
    })
  },
  // 初始化被选校区
  _initCampusInfo() {
    const list = this.data.campus
    list.map(item => {
      item.swiperCurrent ?
        this.setData({
          campusId: item.id,
          campusName: item.campus_name
        }) :
        ''
    })
  },

  // 获取校区
  getCampus() {
    const _this = this
    app.request('campus/getCampus', {}, false, true).then(succ => {
      this._initcampus(succ.data.campus)
      this.getCurricula(succ.data.campus[0].id)
    })
  },
  // 初始化校区数据
  _initcampus(data) {
    const result = []
    data.map((item, index) => {
      index === 0 ?
        result.push(
          Object.assign({}, item, {
            swiperCurrent: true
          })
        ) :
        result.push(
          Object.assign({}, item, {
            swiperCurrent: false
          })
        )
    })
    this.setData({
      campus: result
    })
  },

  // 获取课程分类
  getCurricula(id) {
    const _this = this
    app.request('reserve/getCurricula', { campus_id: id}, false, true).then(succ => {
      this._initProject(succ.data.curricula)
    })
  },
  // 初始化课程信息
  _initProject(data) {
    const result = []
    for (let item of data) {
      let list = item.list
      list = list.map((subItem, index) => {
        subItem.introduce = subItem.introduce.length > 60 ? subItem.introduce.substring(0, 47) + '...' : subItem.introduce
        return (subItem = index === 0 ?
          Object.assign({}, subItem, {
            checked: true
          }) :
          Object.assign({}, subItem, {
            checked: false
          }))
      })
      result.push(
        Object.assign({}, {
          list,
          type_id: item.type_id,
          type_name: item.type_name
        })
      )
    }
    this.setData({
      infoArray: result
    })
  }
})